﻿Public Class DialogMenuProduk
    Dim ParamFinder() As Object
    Public Property ParamCari() As Object()
        Get
            Return ParamFinder
        End Get
        Set(ByVal value As Object())
            ParamFinder = value
        End Set
    End Property
    Private Sub btnCloseItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseItem.Click
        Me.Close()
    End Sub

    Sub searchitem(ByVal DG As DataGridView)
        Dim dtitem As New DataTable
        If Utils.executeSP("proc_searchall", ParamFinder, dtitem) Then
            If dtitem IsNot Nothing Then
                DG.DataSource = dtitem
            End If
        End If
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub DialogMenuProduk_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ParamFinder = New Object() {"idproduk, namaproduk, groupproduk, namagroupproduk, costitemproduk(idproduk) as costproduk", "vproduk", "namaproduk", txtKunci.Text}
        searchitem(DGVItem)
        ParamFinder = New Object() {" idproduk, namaproduk, qtydetail, costitemproduk, costproduk", "vitemmenudetail", "iditemmenu", viditemmenu.Text}
        searchitem(DGVDetail)
    End Sub

    Private Sub hititem()
        Dim hasil As Double = 0
        Try
            If DGVDetail.Rows.Count > 0 Then
                For Each dr As DataGridViewRow In DGVDetail.Rows
                    hasil += dr.Cells("costproduk").Value
                Next
            End If
        Catch ex As Exception

        End Try
        
        TotalCost.Text = hasil
        TotalItem.Text = DGVDetail.RowCount
    End Sub
    Private Sub MbGlassButton17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton17.Click
        Dim dt As New DataTable
        Dim str As String
        Dim DialogInput As New DialogInput
        DialogInput.ShowDialog()
        str = DialogInput.retval
        'Try
        '    str = InputBox("jumlah :-> ", "Jumlah Item", "1")
        'Catch ex As Exception
        '    str = "0"
        '    MsgBox("Maaf Jumlah Item Mohon diisi > 0")
        '    Exit Sub
        'End Try
        If str = "" Or str = "0" Then
            MsgBox("Maaf Jumlah Item Mohon diisi > 0")
            Exit Sub
        End If
        If DGVDetail.DataSource IsNot Nothing Then
            dt = New DataTable
            Dim dtdetail As New DataTable
            dt = DGVDetail.DataSource
            Dim r As DataRow
            r = dt.NewRow
            Dim rdg As New DataGridViewRow
            rdg = DGVItem.CurrentRow
            r(0) = rdg.Cells("idproduk").Value
            r(1) = rdg.Cells("namaproduk").Value
            r(2) = str
            r(3) = rdg.Cells("costproduk").Value
            r(4) = rdg.Cells("costproduk").Value * CDbl(str)
            'costitemproduk, costproduk
            Dim rexists() As DataRow
            rexists = Nothing
            Try
                rexists = dt.Select("idproduk='" & rdg.Cells("idproduk").Value & "'")
            Catch ex As Exception

            End Try


            If rexists Is Nothing Or rexists.Length = 0 Then
                dt.Rows.Add(r)
                dt.AcceptChanges()
                DGVDetail.Refresh()
            Else
                Dim jml As Double
                Dim harga As Double
                jml = rexists(0)("qtydetail")
                jml = jml + CDbl(str)
                harga = jml * rdg.Cells("costproduk").Value
                r(2) = jml
                r(4) = harga

                For Each dr As DataGridViewRow In DGVDetail.Rows
                    If dr.Cells("idproduk").Value = r("idproduk") Then
                        dr.Cells("qtydetail").Value = r(2)
                        dr.Cells("costproduk").Value = r(4)
                        Exit For
                    End If
                Next
                dt.AcceptChanges()
            End If
            hititem()
        End If
    End Sub

    Private Sub MbGlassButton16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton16.Click
        If DGVDetail.RowCount > 0 Then
            Dim rdg As New DataGridViewRow
            rdg = DGVDetail.CurrentRow
            DGVDetail.Rows.Remove(rdg)
        End If
    End Sub

    Private Sub DGVDetail_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDetail.CellDoubleClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        Dim drg As New DataGridViewRow
        drg = DGVDetail.CurrentRow

        Dim DialogInput As New DialogInput
        DialogInput.JmlItem.Text = drg.Cells("qtydetail").Value
        DialogInput.ShowDialog()
        Dim str As String = ""
        str = DialogInput.JmlItem.NumericText
        If Not str = "" Or Not str = "0" Then
            drg.Cells("qtydetail").Value = DialogInput.JmlItem.NumericText
            drg.Cells("costproduk").Value = drg.Cells("costitemproduk").Value * drg.Cells("qtydetail").Value
        End If
        Dim dt As New DataTable
        dt = DGVDetail.DataSource
        dt.AcceptChanges()
        hititem()
    End Sub

    Private Sub MbGlassButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton1.Click
        Dim dt As New DataTable
        If Utils.exec_SP("proc_itemmenudetail", New Object() {"deleteall", viditemmenu.Text, Convert.DBNull, Convert.DBNull}) Then

        End If
        If DGVDetail.RowCount > 0 Then
            dt = New DataTable
            dt = DGVDetail.DataSource
            For Each r As DataRow In dt.Rows
                If Utils.exec_SP("proc_itemmenudetail", New Object() {"insert", viditemmenu.Text, r("idproduk"), r("qtydetail")}) Then

                End If
            Next
            dt = New DataTable
            If Utils.executeSP("proc_searchallEqual", New Object() {"count(*) as hasil", "vitemmenudetail", "iditemmenu", viditemmenu.Text}, dt) Then
                If dt IsNot Nothing Then
                    If dt.Rows.Count > 0 Then
                        MsgBox("Execute Finish" & vbCrLf & "Record Execute = " & IIf(IsDBNull(dt(0)(0)), "0", dt(0)(0)))
                    End If
                End If
            End If
        End If
    End Sub
End Class